package com.jyr.ohs.common;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author ZhongJing </p>
 * @Description 文件操作工具类 </p>
 * @date 2021/5/7 5:38 下午 </p>
 */
public class FileUtils {

    public static String write(MultipartFile file, String filePath, String dirPath) throws IOException {
        // 判断目录是否存在，不存在就创建
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
            System.out.println(dirPath + "文件夹不存在，已创建……");
        }

        InputStream inputStream = file.getInputStream();
        FileOutputStream fos = new FileOutputStream(filePath);

        byte[] data = new byte[1024];
        while (true) {
            int len = inputStream.read(data);
            if (len == -1) {
                break;
            }
            fos.write(data, 0, len);
        }

        return filePath;
    }

}
